package com.simen.kv;

import android.util.Log;

/**
 * 构建where条件
 * Created by ABC on 2017/4/13.
 */
public final class Where {
    public final static int ASC = 1;
    public final static int DESC = 2;
    private static final String TAG = "Where";
    private final static String Separator = " ";
    private final static String And = " and ";
    private final static String Or = " or ";
    private final StringBuilder sql = new StringBuilder();

    public Where equals(String value, String column) {
        addWhere();
        sql.append(Separator);
        sql.append(column);
        sql.append("=");
        sql.append(String.format("\'%s\'",value));
        return this;
    }

    public Where beforeId(String id, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns._ID);
        sql.append(equals ? "<=" : "<");
        sql.append(String.format("\'%s\'",id));
        return this;
    }

    public Where afterId(String id, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns._ID);
        sql.append(equals ? ">=" : ">");
        sql.append(String.format("\'%s\'",id));
        return this;
    }

    public Where beforeCreate(String time, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns.CREATETIMENAME);
        sql.append(equals ? "<=" : "<");
        sql.append(String.format("\'%s\'",time));
        return this;
    }

    public Where afterCreate(String time, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns.CREATETIMENAME);
        sql.append(equals ? ">=" : ">");
        sql.append(String.format("\'%s\'",time));
        return this;
    }

    public Where beforeUpdate(String time, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns.UPDATETIMENAME);
        sql.append(equals ? "<=" : "<");
        sql.append(String.format("\'%s\'",time));
        return this;
    }

    public Where afterUpdate(String time, boolean equals) {
        addWhere();
        sql.append(Separator);
        sql.append(Columns.UPDATETIMENAME);
        sql.append(equals ? ">=" : ">");
        sql.append(String.format("\'%s\'",time));
        return this;
    }

    public Where And() {
        sql.append(And);
        return this;
    }

    public Where Or() {
        sql.append(Or);
        return this;
    }

    /**
     * 指定搜索结果的排序规则
     *
     * @param sort    ASC升序排列,DESC降序排列
     * @param columns 排序的列名称，支持多个
     * @return 返回当前Where对象
     */
    public Where orderBy(int sort, String... columns) {
        sql.append(Separator);
        sql.append("order by ");
        for (int index = 0; index < columns.length; index++) {
            sql.append(columns[index]);
            sql.append(",");
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(Separator);
        switch (sort) {
            case DESC:
                sql.append("desc");
                break;
            case ASC:
            default://默认按照升序
                sql.append("asc");
                break;
        }
        return this;
    }

    /**
     * 添加限制条件，指定从搜索结果中的offset位置开始，取出count数量的数据
     *
     * @param count  指定取出count数量
     * @param offset 确定从哪个位置开始
     * @return 返回当前Where对象
     */
    public Where sub(int offset, int count) {
        sql.append(Separator);
        sql.append("limit ");
        sql.append(count);
        sql.append(Separator);
        sql.append("offset ");
        sql.append(offset);
        return this;
    }

    private void addWhere() {
        String sqlStr = sql.toString();
        if (sqlStr.startsWith(Separator + "limit ")) {
            Log.e(TAG, "you can't set limit before where.");
        }

        if (!sqlStr.startsWith(Separator + "where")) {
            sql.append(Separator);
            sql.append("where");
        }
    }

    /**
     * 返回格式化后的sql语句
     *
     * @return 格式化的where子语句
     */
    public String toSql() {
        return sql.toString();
    }

}
